package com.quiz.bis.controller;

import cn.hutool.core.util.ObjectUtil;
import com.quiz.bis.domain.bo.BisCategorysSearchBo;
import com.quiz.bis.domain.model.BisCategories;
import com.quiz.bis.service.IBisCategoriesService;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ahuan
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/question/category")
@Tags(value = @Tag(name = "题目分类控制层"))
public class CategorysController extends BaseController {

    private final IBisCategoriesService bisCategoriesService;

    /**
     * 获取题目分类列表
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('question:category:list')")
    public R<List<BisCategories>> list(BisCategorysSearchBo categorysSearchBo) {
        return R.ok(bisCategoriesService.list(categorysSearchBo));
    }

    /**
     * 获取题目分类详情
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/{categoryId}")
    @PreAuthorize("@ss.hasPermi('question:category:query')")
    public R<BisCategories> detail(@PathVariable("categoryId") Integer categoryId) {
        return R.ok(bisCategoriesService.getById(categoryId));
    }

    /**
     * 新增分类
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('question:category:add')")
    public R<Void> add(@RequestBody BisCategories bisCategories) {
        return toR(bisCategoriesService.save(bisCategories));
    }

    /**
     * 修改分类
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('question:category:edit')")
    public R<Void> edit(@RequestBody BisCategories bisCategories) {
        return toR(bisCategoriesService.updateById(bisCategories));
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{categoryId}")
    @PreAuthorize("@ss.hasPermi('question:category:delete')")
    public R<Void> remove(@PathVariable("categoryId") Long categoryId) {
        if (ObjectUtil.isNull(categoryId)) {
            return R.fail("分类ID不能为空");
        }
        return toR(bisCategoriesService.removeById(categoryId));
    }


}
